package com.squirrel.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wangyan
 */
public class CreateSelectSql extends AbstractCreateSql implements ICreateSelectSql {

    private static final Logger log = LoggerFactory.getLogger(CreateSelectSql.class);

    private String distinct;
    private String count;
    private String[] fields;
    private String tableName;
    private String orderbyField;
    private String orderbyColline;
    private String fieldIsNull;
    private String fieldIsNotNull;
    private List<Map<String, String>> joinList;
    private List<Map<String, String>> onList;


    public CreateSelectSql() {
        this.joinList = new ArrayList<>();
        this.onList = new ArrayList<>();
    }

    @Override
    public final String getDistinct() {
        return distinct;
    }

    @Override
    public final void setDistinct() {
        this.distinct = "distinct ";
    }

    @Override
    public String getCount() {
        return count;
    }

    @Override
    public void setCount() {
        this.count = "count(0) as count ";
    }

    @Override
    public final void setQueryFields(String... fields) {
        this.fields = fields;
    }

    @Override
    public final String[] getFields() {
        return this.fields;
    }

    @Override
    public final void setQueryTable(String tableName) {
        this.tableName = tableName;
    }

    @Override
    public final String getTableName() {
        return this.tableName;
    }

    @Override
    public final void setJoin(String joinType, String joinTableName) {
        Map<String, String> joinMap = new HashMap<>();
        joinMap.put("condition", joinType);
        joinMap.put("joinTableName", joinTableName);
        this.joinList.add(joinMap);
    }

    @Override
    public final List<Map<String, String>> getJoinTable() {
        return this.joinList;
    }

    @Override
    public final void setOn(String a, String b) {
        Map<String, String> onMap = new HashMap<>();
        onMap.put("mainTableField", a);
        onMap.put("joinTableField", b);
        this.onList.add(onMap);
    }

    @Override
    public final List<Map<String, String>> getOn() {
        return this.onList;
    }

    @Override
    public final String getOrderbyField() {
        return orderbyField;
    }

    @Override
    public final void setOrderbyField(String orderbyField) {
        this.orderbyField = orderbyField;
    }

    @Override
    public final String getOrderbyColline() {
        return orderbyColline;
    }

    @Override
    public final void setOrderbyColline(String orderbyColline) {
        if (this.getOrderbyField() == null) {
            log.error("请先设置需要排序的字段");
        }
        this.orderbyColline = orderbyColline;
    }

    @Override
    public final String getFieldIsNull() {
        return fieldIsNull;
    }

    @Override
    public final void setFieldIsNull(String fieldIsNull) {
        this.fieldIsNull = fieldIsNull;
    }

    @Override
    public final String getFieldIsNotNull() {
        return fieldIsNotNull;
    }

    @Override
    public final void setFieldIsNotNull(String fieldIsNotNull) {
        this.fieldIsNotNull = fieldIsNotNull;
    }
}
